/**
 * 剑指 Offer 24. 反转链表
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
 */
const { displayListNode, createListNode } = require('../../libs/public');

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
function reverseList(head) {
  if (!head) {
    return head;
  }
  let pre = head;
  let { next } = head;
  pre.next = null;
  while (next) {
    const temp = next;
    next = next.next;
    temp.next = pre;
    pre = temp;
  }
  return pre;
}

console.log(displayListNode(reverseList(createListNode([1, 2, 3, 4, 5]))) === '[5,4,3,2,1]');
console.log(displayListNode(reverseList(createListNode([1]))) === '[1]');
console.log(displayListNode(reverseList(createListNode([]))) === '[]');
